''' 1加密数据
2。算法
3。密钥'''
from flask_shop.models import User
from  itsdangerous import TimedJSONWebSignatureSerializer as ts
from flask import current_app,request
from flask_shop.utils.message import to_dict_msg
import functools

def g_token(uid,expiration):
    s=ts(current_app.config["SECRET_KEY"],expires_in=expiration)
    return s.dumps({'id':uid}).decode()
def v_toke(token_str):
    s=ts(current_app.config["SECRET_KEY"])
    try:
        data = s.loads(token)
    except Exception:
        return "awsl"
    user = User.query.filter_by(id = data['id'])
    return user

#登陆token使用
def login_rq(view_func):
    @functools.wraps(view_func)
    def verify_token(*arg,**kwargs):
        try:
            token = request.headers["token"]
        except Exception:
            return to_dict_msg(10016)
        s=ts(current_app.config["SECRET_KEY"])
        try:
            data = s.loads(token)
        except Exception:
            return to_dict_msg(10017)
        return view_func(*arg,**kwargs)
    return verify_token